$(function() {
	
	//启用拖拽
	Common.DragStart($(".diary_detail_header")[0]);

	//关闭按钮
	$(".close").bind("click", function() {
		window.nativeWindow.close();
	});
	//内容高度
	var height = Const.diarydetail._height - 70;
	$(".diary_detail_content").height(height);

	//附件下载
	$(".diary_template_body a").live("click", function() {
		var fileId = this.id.split("_")[1];
		var directory = air.File.documentsDirectory;
		var fileName = $(this).html().trim();
		var fext = $(this).html().split(".")[$(this).html().split(".").length - 1];
		directory.addEventListener(air.Event.SELECT, function() {
			Const.download._filePath = directory.nativePath + "\\" + fileName;
			Const.download._fileId = fileId;
			Common.OpenDownloadWin();
		});
		directory.browseForDirectory(fext);
	});
	//初始化数据
	var diary = JSON.parse(App.getTempData());
	var bakInfo = "";
	var datetime = diary.diaryTime.replace("0:00:00", "");
	$(".diary_detail_time span").html(datetime.split("-")[0] + "年" + datetime.split("-")[1] + "月" + datetime.split("-")[2] + "日");
	$(".diary_detail_from").html("来自" + diary.diaryIP);
	bakInfo += "<div class=\"diary_template_title\">日记内容</div>";
	bakInfo += "<div class='diary_template_body'>";
	for(var j = 0; j < diary.templates.length; j++) {
		bakInfo += "<span style='font-weight:bold;'>" + diary.templates[j].name + "</span>：<br/>";
		for(var k = 0; k < diary.templates[j].diaryNodes.length; k++) {
			bakInfo += (k + 1).toString() + "、";
			bakInfo += diary.templates[j].diaryNodes[k];
			bakInfo += "<br/>";
		}
	}
	bakInfo += "</div>";
	if(diary.attachments.length > 0) {
		bakInfo += "<div class=\"diary_template_title\" style='background:#fff'>附件</div>";
		bakInfo += "<div class='diary_template_body'>"
		for(var j = 0; j < diary.attachments.length; j++) {
			bakInfo += "<a href='#' id='file_" + diary.attachments[j].fileId + "'>" + diary.attachments[j].fileName + "</a>"
		}
		bakInfo += "</div>";
	}

	if(diary.remark.trim() !== "") {
		bakInfo += "<div class=\"diary_template_title\">备注</div>";
		bakInfo += "<div class='diary_template_body'>";
		bakInfo += diary.remark;
		bakInfo += "</div>";
	}
	if(diary.leaderIdea.comment.length > 0 || diary.leaderIdea.share.length > 0) {
		bakInfo += "<div class=\"diary_template_title\">领导批示</div>";
	}
	//领导批示
	for(var j = 0; j < diary.leaderIdea.comment.length; j++) {
		bakInfo += "<div class='diary_template_body'>";
		bakInfo += "<div class='diary_leader'>";
		bakInfo += "<span class='leadertime'>";
		bakInfo += diary.leaderIdea.comment[j].time;
		bakInfo += "</span>";
		bakInfo += "<span class='leader'>";
		bakInfo += diary.leaderIdea.comment[j].leader;
		bakInfo += "：</span>";
		bakInfo += "</div>";
		bakInfo += "<div class='diary_leader_idea'>";
		bakInfo += diary.leaderIdea.comment[j].idea;
		bakInfo += "</div>";
		bakInfo += "</div>";

	}
	//分享批示
	for(var j = 0; j < diary.leaderIdea.share.length; j++) {
		bakInfo += "<div class='diary_template_body'>";
		bakInfo += "<div class='diary_leader'>";
		bakInfo += "<span class='leadertime'>";
		bakInfo += diary.leaderIdea.share[j].time;
		bakInfo += "</span>";
		bakInfo += "<span class='leader'>";
		bakInfo += diary.leaderIdea.share[j].leader;
		bakInfo += "：</span>";
		bakInfo += "</div>";
		bakInfo += "<div class='diary_leader_idea'>";
		bakInfo += diary.leaderIdea.share[j].idea;
		bakInfo += "</div>";
		bakInfo += "</div>";
	}

	$(".diary_detail_content").html(bakInfo);
});
